package com.cincc.taofangwang.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.cincc.taofangwang.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.gyf.immersionbar.ImmersionBar;

public class MainActivity extends AppCompatActivity implements NavigationBarView.OnItemSelectedListener {
    private HomeFragment homeFragment;
    private RecordFragment recordFragment;
    private HistoryFragment historyFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        initView();
    }

    private void initView() {
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnItemSelectedListener(this);
        showHomePage();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_1:
                showHomePage();
                return true;
            case R.id.navigation_2:
                showRecordPage();
                return true;
            case R.id.navigation_3:
                showHistoryPage();
                return true;
        }
        return false;
    }

    private void showHomePage() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
            fragmentTransaction.add(R.id.nav_host_fragment, homeFragment);
        } else {
            fragmentTransaction.show(homeFragment);
        }
        fragmentTransaction.commit();
    }

    private void showRecordPage() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        if (recordFragment == null) {
            recordFragment = new RecordFragment();
            fragmentTransaction.add(R.id.nav_host_fragment, recordFragment);
        } else {
            fragmentTransaction.show(recordFragment);
        }
        fragmentTransaction.commit();
    }

    private void showHistoryPage() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        if (historyFragment == null) {
            historyFragment = new HistoryFragment();
            fragmentTransaction.add(R.id.nav_host_fragment, historyFragment);
        } else {
            fragmentTransaction.show(historyFragment);
        }
        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (homeFragment != null) {
            fragmentTransaction.hide(homeFragment);
        }
        if (recordFragment != null) {
            fragmentTransaction.hide(recordFragment);
        }
        if (historyFragment != null) {
            fragmentTransaction.hide(historyFragment);
        }
    }

    private static final long DOUBLE_CLICK_TIME_DELTA = 2000; // 两次返回键按下的最大时间间隔，单位为毫秒
    private boolean isBackPressedOnce = false; // 是否已经按下一次返回键
    private long lastBackPressedTime = 0; // 上次按下返回键的时间

    @Override
    public void onBackPressed() {
        // 判断是否是第一次按下返回键
        if (!isBackPressedOnce) {
            // 记录当前时间
            lastBackPressedTime = System.currentTimeMillis();
            isBackPressedOnce = true;
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
        } else {
            // 获取当前时间
            long currentBackPressedTime = System.currentTimeMillis();
            // 判断两次按下的时间间隔是否小于规定的时间
            if (currentBackPressedTime - lastBackPressedTime <= DOUBLE_CLICK_TIME_DELTA) {
                // 退出应用
                finish();
            } else {
                // 重置计时器
                isBackPressedOnce = false;
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            }
        }
    }
}